<?php

namespace frontend\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "address".
 *
 * @property integer $id
 * @property integer $member_id
 * @property string $username
 * @property string $province_name
 * @property string $city_name
 * @property string $area_name
 * @property string $detail_address
 * @property string $mobilephone
 * @property integer $status
 * @property integer $created_at
 * @property integer $updated_at
 */
class Address extends \yii\db\ActiveRecord
{

    /**
     * 附加行为 系统的行为
     * @return array
     */
    public function behaviors()
    {
        return [
            //给行为命名
            'time'=>[
                //一个行为一个数组
                'class'=>TimestampBehavior::className(),
                'attributes'=>[
                    //插入之前执行的事件
                    ActiveRecord::EVENT_BEFORE_INSERT=>['created_at','updated_at'],
                    //更新之前执行的事件
                    ActiveRecord::EVENT_BEFORE_UPDATE=>['updated_at'],
                ],


            ],
        ];
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'address';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [[ 'username', 'province_name', 'city_name', 'area_name', 'detail_address', 'mobilephone'], 'required'],
            [['member_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['username', 'detail_address'], 'string', 'max' => 50],
            [['province_name', 'city_name', 'area_name'], 'string', 'max' => 30],
            [['mobilephone'], 'string', 'max' => 12],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'member_id' => '会员id',
            'username' => '收货人',
            'province_name' => '省份',
            'city_name' => '城市',
            'area_name' => '区域',
            'detail_address' => '详细地址',
            'mobilephone' => '手机号码',
            'status' => '状态1默认地址0正常-1删除状态',
            'created_at' => '添加时间',
            'updated_at' => '更新时间',
        ];
    }




    public static function defualtAddress($id){
        //把当前地址涩味默认地址(status=1),其他设为普通地址(status=0)
        //首先把所有的状态值归0
        \Yii::$app->db->createCommand()->update('address',['status'=>0],['member_id'=>Yii::$app->user->id])->execute();
        //把当前地址状态值设为1
        $model = Address::findOne(['id'=>$id]);
        $model->status = 1;
        return  $model->update();
    }

    /**
     * 获取城市信息
     */
    public static function FirstCity($parent_id=0){
        $query = new Query();
        $cityInfo = $query->select('*')->from('locations')->where(['parent_id'=>$parent_id])->all();
        $cityInfo = ArrayHelper::map($cityInfo,'id','name');
        return $cityInfo;
    }

    public static function AddressArray(){
        $member_id = Yii::$app->user->id;
        return Address::find()->where(['member_id'=>$member_id])->asArray()->all();
    }


}
